package com.jblog.core.dao.base;

import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.util.List;

import org.springframework.dao.DataAccessException;

import com.jblog.Constants;
import com.jblog.core.model.BaseParam;
import com.jblog.core.spring.MySqlSessionDaoSupport;

@SuppressWarnings("unchecked")
public abstract class EntityDao4Mybatis<E, PK extends Serializable> extends
        MySqlSessionDaoSupport implements EntityDao<E, PK> {

    protected String namespace;

    public EntityDao4Mybatis() {
        // 通过范型反射，取得在子类中定义的class.
        Class<E> clazz = (Class<E>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
        namespace = clazz.getName().replaceAll(".*model\\.", "") + ".";
    }
    
    protected String getNamespace() {
        return this.namespace;
    }
//
//    @Override
//    public E get(PK pk) throws DataAccessException {
//        return (E) super.getSqlSession().selectOne(namespace + Constants.MyBatis.GET_BY_ID, pk);
//    }
//    
//    @Override
//    public E get(String s) throws DataAccessException {
//        return (E) super.getSqlSession().selectOne(namespace + Constants.MyBatis.GET_BY_ALIAS, s);
//    }
//
//    @Override
//    public List<E> byEntity(E e) throws DataAccessException {
//        return super.getSqlSession().selectList(namespace + Constants.MyBatis.GET_BY_ENTITY, e);
//    }
    @Override
    public E get() throws DataAccessException {
        return (E)super.getSqlSession().selectOne(namespace + Constants.MyBatis.GET);
    }

    @Override
    public Long count(BaseParam p) throws DataAccessException {
        return Long.valueOf(super.getSqlSession().selectOne(namespace + Constants.MyBatis.GET_COUNT, p).toString());
    }

    @Override
    public List<E> byParam(BaseParam p) throws DataAccessException {
        return super.getSqlSession().selectList(namespace + Constants.MyBatis.GET_BY_PARAM, p);
    }

    @Override
    public void save(E e) throws DataAccessException {
        super.getSqlSession().insert(namespace + Constants.MyBatis.SAVE, e);        
    }

    @Override
    public void saveBatch(List<E> es) throws DataAccessException {
        if(es.size() > 1) {
            super.getSqlSession().insert(namespace + Constants.MyBatis.SAVE_BATCH, es);        
        } else if (es.size() > 0) {
            save(es.get(0));
        }
    }

    @Override
    public void update(E e) throws DataAccessException {
        super.getSqlSession().update(namespace + Constants.MyBatis.UPDATE, e);
    }

    @Override
    public void deleteByPk(PK pk) throws DataAccessException {
        super.getSqlSession().delete(namespace + Constants.MyBatis.DELETE_BY_PK, pk);
    }

    @Override
    public void deleteBatchByPks(List<PK> pks) throws DataAccessException {
        if(pks.size() > 1) {
            super.getSqlSession().delete(namespace + Constants.MyBatis.DELETE_BATCH_BY_PKS, pks);
        } else if (pks.size() > 0) {
            deleteByPk(pks.get(0));
        }
    }
//
//    @Override
//    public void deleteByEntity(E e) throws DataAccessException {
//        super.getSqlSession().delete(namespace + Constants.MyBatis.DELETE_BY_ENTITY, e);
//    }

}
